package com.web_bloks.generators.rebind;

import java.util.Vector;

import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;

import com.web_bloks.generators.client.ScreenModule;

/**
 * Keeps a list of all the sub-classes of 'ScreenModule'
 * 
 * @author Jean-Lou Dupont
 *
 */
public class ScreenModuleManager extends Generator {
	
	// The list of ScreenModules we collect along
	public static Vector<String> liste = new Vector<String>();
	
	public String generate(TreeLogger logger, GeneratorContext context, String typeName) 
		throws UnableToCompleteException {

	    JClassType smType = null;
	    try {
	      smType = context.getTypeOracle().getType(ScreenModule.class.getName());
	    } catch (NotFoundException e) {
	      logger.log(TreeLogger.ERROR, "Cannot find ScreenModule class", e);
	      throw new UnableToCompleteException();
	    }
	    
	    // we want to sub-classes of ScreenModule
	    JClassType[] types = smType.getSubtypes();

	    // Generate the source and raw source files
	    for (JClassType type : types) {
	    	logger.log(TreeLogger.Type.INFO, "found ["+type.toString()+"]");
	    	
	    	// make sure we only collect it once!
	    	if ( !liste.contains( type )) {
	    		liste.add(type.toString());
	    	}
	    }

	    // we ain't changing no type on the fly here.
		return null;
	}
	
}//END